package edu.uta.cse6339.facetedinterface.read.article.labels;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
public class ReadClassfiedArticleLabels {

	
	public static HashMap<String,Set<String>> readArticleLabels(String fileName){
		HashMap<String,Set<String>> classifedArticles = new HashMap<String,Set<String>>();
		String line=null;
		if(fileName!=null){
			File file = new File(fileName);
			if(file.exists()){
				BufferedReader reader;
				try {
					reader = new BufferedReader(new FileReader(file));
					if(reader!=null){
						while((line=reader.readLine())!=null && line.length()>1){
							Set<String> valuesSet = new HashSet<String>();
							if(line!=null){
								line=line.trim();
								String keyValues[]=line.split(";");
								if(keyValues!=null && keyValues.length>0){
									String key = (keyValues[0]!=null ? keyValues[0].trim():"");
									String values[] =null;
									if(keyValues[1]!=null){
										values = keyValues[1].split(",");
										for(String value : values){
											if(value!=null)
												valuesSet.add(value.trim().toLowerCase());
										}	
									}
									classifedArticles.put(key, valuesSet);
								}
							}
						}
					}
				}
				catch (FileNotFoundException e) {
					System.out.println("Error opening classified article label file"+fileName);
					e.printStackTrace();
				} catch (IOException e) {
					System.out.println("Error reading classified article label file"+fileName);
					e.printStackTrace();
				}
			}
		}
		return classifedArticles;
	}
	
	public static void main(String args[]){
		HashMap<String,Set<String>> articleLabels =readArticleLabels("src/edu/uta/cse6339/facetedinterface/resources/stateClasifiedArticles");
		if(articleLabels!=null){
			for(String key : articleLabels.keySet()){
				System.out.println(" Key : "+key +" Values :"+articleLabels.get(key));
			}
			
		}
	}
}
